package com.molichuxing.gateway.bff.log.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.log.entity.response.DrivingLicenseNotifyLogVo;
import com.molichuxing.gateway.bff.log.entity.response.IdverifyNotifyLogVo;
import com.molichuxing.gateway.bff.log.entity.response.ThreeverifyNotifyLogVo;
import com.molichuxing.gateway.bff.log.services.LogBffService;
import com.molichuxing.gateway.property.DrivingLicenseLogEnum;
import com.molichuxing.gateway.property.IdverifyLogEnum;
import com.molichuxing.gateway.property.ThreeverifyLogEnum;
import com.molichuxing.services.infrastructure.dto.response.GztDrivingLicenseNotifyLogDto;
import com.molichuxing.services.infrastructure.dto.response.GztIdverifyNotifyLogDto;
import com.molichuxing.services.infrastructure.dto.response.GztThreeverifyNotifyLogDto;
import com.molichuxing.services.infrastructure.dto.response.PyDrivingLicenseNotifyLogDto;
import com.molichuxing.services.infrastructure.dto.response.PyIdverifyNotifyLogDto;
import com.molichuxing.services.infrastructure.dto.response.PyThreeverifyNotifyLogDto;
import com.molichuxing.services.infrastructure.service.GztDrivingLicenseNotifyLogService;
import com.molichuxing.services.infrastructure.service.GztIdverifyNotifyLogService;
import com.molichuxing.services.infrastructure.service.GztThreeverifyNotifyLogService;
import com.molichuxing.services.infrastructure.service.PyDrivingLicenseNotifyLogService;
import com.molichuxing.services.infrastructure.service.PyIdverifyNotifyLogService;
import com.molichuxing.services.infrastructure.service.PyThreeverifyNotifyLogService;

@Service("logBffService")
public class LogBffServiceImpl implements LogBffService{

	
	@Resource
	private PyIdverifyNotifyLogService pyIdverifyNotifyLogService;
	
	@Resource
	private GztIdverifyNotifyLogService gztIdverifyNotifyLogService;
	
	@Resource
	private PyThreeverifyNotifyLogService pyThreeverifyNotifyLogService;
	
	@Resource
	private GztThreeverifyNotifyLogService gztThreeverifyNotifyLogService;
	
	@Resource
	private PyDrivingLicenseNotifyLogService pyDrivingLicenseNotifyLogService;
	
	@Resource
	private GztDrivingLicenseNotifyLogService gztDrivingLicenseNotifyLogService;
	
	/**
	 * 获取身份验证日志列表
	 * @param map
	 *            参数
     *key:tabType Integer tab类型
	 *            
	 *key:idNumber
	 *            String 身份证号 非必填
	 *key:realName
	 *            String 用户真实姓名 非必填
	 *key:startTime
	 *            String 请求开始时间 非必填
	 *key:endTime
	 *            String 请求结束时间 非必填
	 *key:pageNum
	 *            Integer 当前页
	 *key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 * @throws Exception
	 */
	@Override
	public Paged<IdverifyNotifyLogVo> getIdverifyNotifyLogList(Map<String, Object> map) throws Exception {
		//封装tab
		Paged<IdverifyNotifyLogVo> paged=getIdverifyTab();
		//获取参数
		Integer tabType = ObjectCastUtil.castInteger(map.get("tabType"));
		if( null == tabType || IdverifyLogEnum.gzt.getValue() == tabType ) {
			paged=getGztIdverifyNotifyLogList(paged,map);
		}else {
			paged=getPyIdverifyNotifyLogList(paged,map);
		}
		return paged;
	}
	
	/**
	 * 国政通身份认证日志
	 * @param paged
	 * @param map
	 * @return
	 */
	private Paged<IdverifyNotifyLogVo> getGztIdverifyNotifyLogList(Paged<IdverifyNotifyLogVo> paged,Map<String, Object> map){
		Paged<GztIdverifyNotifyLogDto> gztIdverifyNotifyLogList = gztIdverifyNotifyLogService.getGztIdverifyNotifyLogList(map);
		if(null == gztIdverifyNotifyLogList) {
			return paged;
		}else if(gztIdverifyNotifyLogList.isEmpty()) {
			paged.setTotal(gztIdverifyNotifyLogList.getTotal());
			paged.setPageSize(gztIdverifyNotifyLogList.getPageSize());
			return paged;
		}
		List<GztIdverifyNotifyLogDto> list = gztIdverifyNotifyLogList.getList();
		List<IdverifyNotifyLogVo> voList = new ArrayList<>();
		for (GztIdverifyNotifyLogDto dto : list) {
			IdverifyNotifyLogVo vo = new IdverifyNotifyLogVo();
			BeanUtils.copyProperties(dto, vo);
			voList.add(vo);
		}
		paged.setList(voList);
		paged.setPageSize(gztIdverifyNotifyLogList.getPageSize());
		paged.setTotal(gztIdverifyNotifyLogList.getTotal());
		return paged;
	}
	
	
	/**
	 * 国政通身份认证日志
	 * @param paged
	 * @param map
	 * @return
	 */
	private Paged<IdverifyNotifyLogVo> getPyIdverifyNotifyLogList(Paged<IdverifyNotifyLogVo> paged,Map<String, Object> map){
		Paged<PyIdverifyNotifyLogDto> pyIdverifyNotifyLogList = pyIdverifyNotifyLogService.getPyIdverifyNotifyLogList(map);
		if(null == pyIdverifyNotifyLogList) {
			return paged;
		}else if(pyIdverifyNotifyLogList.isEmpty()) {
			paged.setTotal(pyIdverifyNotifyLogList.getTotal());
			paged.setPageSize(pyIdverifyNotifyLogList.getPageSize());
			return paged;
		}
		List<PyIdverifyNotifyLogDto> list = pyIdverifyNotifyLogList.getList();
		List<IdverifyNotifyLogVo> voList = new ArrayList<>();
		for (PyIdverifyNotifyLogDto dto : list) {
			IdverifyNotifyLogVo vo = new IdverifyNotifyLogVo();
			BeanUtils.copyProperties(dto, vo);
			voList.add(vo);
		}
		paged.setList(voList);
		paged.setPageSize(pyIdverifyNotifyLogList.getPageSize());
		paged.setTotal(pyIdverifyNotifyLogList.getTotal());
		return paged;
	}
	
	/**
	 * 获取身份认证tab
	 * @param map
	 * @param paged
	 * @return
	 */
	private Paged<IdverifyNotifyLogVo> getIdverifyTab() {
		Paged<IdverifyNotifyLogVo> paged=new Paged<>();
		List<TabDto> tab=new ArrayList<TabDto>(2);
		IdverifyLogEnum[] values = IdverifyLogEnum.values();
		for (IdverifyLogEnum logEnum : values) {
			TabDto tabDto=new TabDto();
			tabDto.setType(logEnum.getValue());
			tabDto.setName(logEnum.getName());
			tab.add(tabDto);
		}
		paged.setTab(tab);
        return paged;
	}
	/**
	 * 获取三要素tab
	 * @param map
	 * @param paged
	 * @return
	 */
	private Paged<ThreeverifyNotifyLogVo> getThreeverifyTab() {
		Paged<ThreeverifyNotifyLogVo> paged=new Paged<>();
		List<TabDto> tab=new ArrayList<TabDto>(2);
		ThreeverifyLogEnum[] values = ThreeverifyLogEnum.values();
		for (ThreeverifyLogEnum logEnum : values) {
			TabDto tabDto=new TabDto();
			tabDto.setType(logEnum.getValue());
			tabDto.setName(logEnum.getName());
			tab.add(tabDto);
		}
		paged.setTab(tab);
		return paged;
	}
	/**
	 * 获取驾驶证tab
	 * @param map
	 * @param paged
	 * @return
	 */
	private Paged<DrivingLicenseNotifyLogVo> getDrivingLicenseTab() {
		Paged<DrivingLicenseNotifyLogVo> paged=new Paged<>();
		List<TabDto> tab=new ArrayList<TabDto>(2);
		DrivingLicenseLogEnum[] values = DrivingLicenseLogEnum.values();
		for (DrivingLicenseLogEnum logEnum : values) {
			TabDto tabDto=new TabDto();
			tabDto.setType(logEnum.getValue());
			tabDto.setName(logEnum.getName());
			tab.add(tabDto);
		}
		paged.setTab(tab);
		return paged;
	}
	
	
	/**
	 * 获取三要素日志列表
	 * @param map
	 *            参数
     *key:tabType Integer tab类型
	 *            
	 *key:idNumber
	 *            String 身份证号 非必填
	 *key:realName
	 *            String 用户真实姓名 非必填
	 *key:userPhone
	 *            String 用户手机号 非必填
	 *key:startTime
	 *            String 请求开始时间 非必填
	 *key:endTime
	 *            String 请求结束时间 非必填
	 *key:pageNum
	 *            Integer 当前页
	 *key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 * @throws Exception
	 */
	@Override
	public Paged<ThreeverifyNotifyLogVo> getThreeverifyNotifyLogList(Map<String, Object> map) throws Exception {
		//封装tab
		Paged<ThreeverifyNotifyLogVo> paged=getThreeverifyTab();
		//获取参数
		Integer tabType = ObjectCastUtil.castInteger(map.get("tabType"));
		if( null == tabType || ThreeverifyLogEnum.gzt.getValue() == tabType ) {
			paged=getGztThreeverifyNotifyLogList(paged,map);
		}else {
			paged=getPyThreeverifyNotifyLogList(paged,map);
		}
		return paged;
	}
	
	
	/**
	 * 国政通三要素日志
	 * @param paged
	 * @param map
	 * @return
	 */
	private Paged<ThreeverifyNotifyLogVo> getGztThreeverifyNotifyLogList(Paged<ThreeverifyNotifyLogVo> paged,Map<String, Object> map){
		Paged<GztThreeverifyNotifyLogDto> threeverifyNotifyLogList = gztThreeverifyNotifyLogService.getGztThreeverifyNotifyLogList(map);
		
		if(null == threeverifyNotifyLogList) {
			return paged;
		}else if(threeverifyNotifyLogList.isEmpty()) {
			paged.setTotal(threeverifyNotifyLogList.getTotal());
			paged.setPageSize(threeverifyNotifyLogList.getPageSize());
			return paged;
		}
		List<GztThreeverifyNotifyLogDto> list = threeverifyNotifyLogList.getList();
		List<ThreeverifyNotifyLogVo> voList = new ArrayList<>();
		for (GztThreeverifyNotifyLogDto dto : list) {
			ThreeverifyNotifyLogVo vo = new ThreeverifyNotifyLogVo();
			BeanUtils.copyProperties(dto, vo);
			voList.add(vo);
		}
		paged.setList(voList);
		paged.setPageSize(threeverifyNotifyLogList.getPageSize());
		paged.setTotal(threeverifyNotifyLogList.getTotal());
		return paged;
	}
	
	/**
	 * 鹏元三要素日志
	 * @param paged
	 * @param map
	 * @return
	 */
	private Paged<ThreeverifyNotifyLogVo>  getPyThreeverifyNotifyLogList(Paged<ThreeverifyNotifyLogVo> paged,Map<String, Object> map){
		Paged<PyThreeverifyNotifyLogDto> threeverifyNotifyLogList = pyThreeverifyNotifyLogService.getPyThreeverifyNotifyLogList(map);
		
		if(null == threeverifyNotifyLogList) {
			return paged;
		}else if(threeverifyNotifyLogList.isEmpty()) {
			paged.setTotal(threeverifyNotifyLogList.getTotal());
			paged.setPageSize(threeverifyNotifyLogList.getPageSize());
			return paged;
		}
		List<PyThreeverifyNotifyLogDto> list = threeverifyNotifyLogList.getList();
		List<ThreeverifyNotifyLogVo> voList = new ArrayList<>();
		for (PyThreeverifyNotifyLogDto dto : list) {
			ThreeverifyNotifyLogVo vo = new ThreeverifyNotifyLogVo();
			BeanUtils.copyProperties(dto, vo);
			voList.add(vo);
		}
		paged.setList(voList);
		paged.setPageSize(threeverifyNotifyLogList.getPageSize());
		paged.setTotal(threeverifyNotifyLogList.getTotal());
		return paged;
	}
	
	/**
	 * 获取驾驶证日志列表
	 * @param map
	 *            参数
     *key:tabType Integer tab类型
	 *            
	 *key:idNumber
	 *            String 身份证号 非必填
	 *key:realName
	 *            String 用户真实姓名 非必填
	 *key:startTime
	 *            String 请求开始时间 非必填
	 *key:endTime
	 *            String 请求结束时间 非必填
	 *key:pageNum
	 *            Integer 当前页
	 *key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 * @throws Exception
	 */
	@Override
	public Paged<DrivingLicenseNotifyLogVo> getDrivingLicenseNotifyLogList(Map<String, Object> map) throws Exception {
		//封装tab
		Paged<DrivingLicenseNotifyLogVo> paged=getDrivingLicenseTab();
		//获取参数
		Integer tabType = ObjectCastUtil.castInteger(map.get("tabType"));
		if( null == tabType || DrivingLicenseLogEnum.gzt.getValue() == tabType ) {
			paged=getGztDrivingLicenseNotifyLogList(paged,map);
		}else {
			paged=getPyDrivingLicenseNotifyLogList(paged,map);
		}
		return paged;		
	}
	
	/**
	 * 国政通驾驶证日志
	 * @param paged
	 * @param map
	 * @return
	 */
	private Paged<DrivingLicenseNotifyLogVo> getGztDrivingLicenseNotifyLogList(Paged<DrivingLicenseNotifyLogVo> paged,Map<String, Object> map){
		Paged<GztDrivingLicenseNotifyLogDto> gztDrivingLicenseNotifyLogList = gztDrivingLicenseNotifyLogService.getGztDrivingLicenseNotifyLogList(map);
		
		if(null == gztDrivingLicenseNotifyLogList) {
			return paged;
		}else if(gztDrivingLicenseNotifyLogList.isEmpty()) {
			paged.setTotal(gztDrivingLicenseNotifyLogList.getTotal());
			paged.setPageSize(gztDrivingLicenseNotifyLogList.getPageSize());
			return paged;
		}
		List<GztDrivingLicenseNotifyLogDto> list = gztDrivingLicenseNotifyLogList.getList();
		List<DrivingLicenseNotifyLogVo> voList = new ArrayList<>();
		for (GztDrivingLicenseNotifyLogDto dto : list) {
			DrivingLicenseNotifyLogVo vo = new DrivingLicenseNotifyLogVo();
			BeanUtils.copyProperties(dto, vo);
			voList.add(vo);
		}
		paged.setList(voList);
		paged.setPageSize(gztDrivingLicenseNotifyLogList.getPageSize());
		paged.setTotal(gztDrivingLicenseNotifyLogList.getTotal());
		return paged;
	}
	
	
	/**
	 * 鹏元驾驶证日志
	 * @param paged
	 * @param map
	 * @return
	 */
	private Paged<DrivingLicenseNotifyLogVo> getPyDrivingLicenseNotifyLogList(Paged<DrivingLicenseNotifyLogVo> paged,Map<String, Object> map){
		Paged<PyDrivingLicenseNotifyLogDto> pyDrivingLicenseNotifyLogList = pyDrivingLicenseNotifyLogService.getPyDrivingLicenseNotifyLogList(map);
		
		if(null == pyDrivingLicenseNotifyLogList) {
			return paged;
		}else if(pyDrivingLicenseNotifyLogList.isEmpty()) {
			paged.setTotal(pyDrivingLicenseNotifyLogList.getTotal());
			paged.setPageSize(pyDrivingLicenseNotifyLogList.getPageSize());
			return paged;
		}
		List<PyDrivingLicenseNotifyLogDto> list = pyDrivingLicenseNotifyLogList.getList();
		List<DrivingLicenseNotifyLogVo> voList = new ArrayList<>();
		for (PyDrivingLicenseNotifyLogDto dto : list) {
			DrivingLicenseNotifyLogVo vo = new DrivingLicenseNotifyLogVo();
			BeanUtils.copyProperties(dto, vo);
			voList.add(vo);
		}
		paged.setList(voList);
		paged.setPageSize(pyDrivingLicenseNotifyLogList.getPageSize());
		paged.setTotal(pyDrivingLicenseNotifyLogList.getTotal());
		return paged;
	}
	
}
